package fm.dongman.modellib.source;

import android.support.annotation.NonNull;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.MyContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.local.DataOperationHelper;
import fm.dongman.modellib.local.LoginLocalDataSource;
import fm.dongman.modellib.model.UserModel;
import fm.dongman.modellib.remote.LoginRemoteDataSource;
import fm.dongman.modellib.remote.UserEditRemoteDataSource;

/**
 * 我的页面数据源操作
 * Created by KUIGE on 2017/10/18.
 */

public class MyRepository implements MyContract.IMyDataSource {

    private static MyRepository SINGLETON;

    private LoginRemoteDataSource mRemote;

    private LoginLocalDataSource mLocal;

    private boolean mIsRefresh = false; // 是否刷新
    private boolean mIsOffLine = false; // 是否为离线加载

    public MyRepository(LoginRemoteDataSource instance, LoginLocalDataSource myLocalDataSource) {
        mRemote = instance;
        mLocal = myLocalDataSource;
    }

    public static MyRepository getInstance(LoginRemoteDataSource instance, LoginLocalDataSource myLocalDataSource) {
        if (SINGLETON == null) {
            SINGLETON = new MyRepository(instance, myLocalDataSource);
        }
        return SINGLETON;
    }

    @Override
    public void deleteAccount() {
    }

    @Override
    public void getData(@NonNull final BaseDataSource.LoadSourceCallback<? super IModel.IUserModel> callback) {
        if (mIsRefresh) {
            refreshMyInfo(callback);
            return;
        }
        if (mIsOffLine) {
            // 离线加载数据
            mLocal.getData(callback);
            return;
        }
        // 将 TOKEN 读取到内存
        mLocal.onRefresh();
        if (StaticUrl.RETOKEN_KEY.equals("") || StaticUrl.RETOKEN_KEY == null) {
            callback.onDataLoadFiled();
        } else {
            refreshToken();
        }
        mLocal.getData(callback);
    }

    /**
     * 刷新我的数据
     */
    private void refreshMyInfo(@NonNull final LoadSourceCallback<? super IModel.IUserModel> callback) {
        // 刷新TOKEN数据
        UserEditRemoteDataSource.getInstance().getData(new LoadSourceCallback<IModel.IUserModel>() {
            @Override
            public void onDataLoaded(IModel.IUserModel iUserModel) {
                callback.onDataLoaded(iUserModel);
                saveData(iUserModel);
            }

            @Override
            public void onDataLoadFiled() {
                callback.onDataLoadFiled();
            }
        });
        mIsRefresh = false;
    }

    private void refreshToken() {
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("x-refresh-token", StaticUrl.RETOKEN_KEY);
        HashMap<String, String> map2 = new HashMap<>();
        JSONObject object = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.UP_TOKEN, map1, map2);
        if (object != null) {
            mRemote.refreshD(object);
            mLocal.saveToken(object);
        }
    }

    @Override
    public void saveData(IModel.IUserModel dataModel) {
        mLocal.saveData(dataModel);
    }

    @Override
    public void refreshData() {
        mIsRefresh = true;
    }

    @Override
    public void offLine() {
        mIsOffLine = true;
    }
}
